import type { Router } from 'uni-mini-router';
import { useAuthStore } from '@/state/modules/auth';
const whiteList = ['login', 'home', 'work', 'mine'];

export function userRouternext(router: Router) {
    router.beforeEach((to, from, next) => {
        const authStore = useAuthStore();
        if (authStore.isLogin) {
            next();
        } else {
            if (whiteList.includes(to.name as string)) {
                // console.log('我在白名单里面');
                next();
            } else {
                next({
                    name: 'login',
                });
            }
        }
        // next();
    });

    router.afterEach((to) => {
        const authStore = useAuthStore();
        // console.log(authStore.isLogin, to.name, '跳转了');
        if (!authStore.isLogin && to && to.name !== 'login') {
            if (whiteList.includes(to.name as string)) {
                // console.log('没有登录，但我在白名单里面');
            } else {
                router.push({
                    name: 'login',
                    params: { ...to.query },
                });
            }
        } else if (authStore.isLogin && to && to.name === 'login') {
            router.replaceAll({ name: 'home' });
        }
    });
}
